package com.cloudkeeper.leasing.identity.repository;

import com.cloudkeeper.leasing.identity.domain.KPIVillageStatistics;
import com.cloudkeeper.leasing.base.repository.BaseRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.annotation.Nonnull;
import java.util.List;

/**
 * 村一级指标统计 repository
 * @author yujian
 */
@Repository
public interface KPIVillageStatisticsRepository extends BaseRepository<KPIVillageStatistics> {

    void deleteAllByTaskId(@Nonnull String taskId);

    List<KPIVillageStatistics> findAllByCadresIdAndQuotaLevelOrderByCreatedAt(String cadresId, String quotaLevel);

    @Query(value = "SELECT quotaYear from KPI_Village_Statistics where cadresId = :cadresId and partitionLevel = :partitionLevel and quotaLevel = 0 order by quotaYear asc", nativeQuery = true)
    List<String> getYearsByLevel(@Param("cadresId") String cadresId, @Param("partitionLevel") String partitionLevel);

    @Query(value = "SELECT quotaYear from KPI_Village_Statistics where cadresId = :cadresId and ranking <= :rank and quotaLevel = 0 order by quotaYear asc", nativeQuery = true)
    List<String> getYearsByRank(@Param("cadresId") String cadresId, @Param("rank") int rank);
}